package com.we.wecommon.utils;

import com.we.wecommon.constant.RestConstant.RestContentType;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;


    private int code;
    private String msg;
    private T data;

    public static <T> R<T> ok() {
        return  RestUtil(RestContentType.SUCCESS.getCode(), RestContentType.SUCCESS.getMsg(),null);
    }
    public static <T> R<T> ok(int code) {
        return  RestUtil(code, RestContentType.SUCCESS.getMsg(),null);
    }
    public static <T> R<T> ok(int code, String msg) {
        return  RestUtil(code, msg,null);
    }
    public static <T> R<T> ok(int code, String msg, T data) {
        return  RestUtil(code, msg,data);
    }

    public static <T> R<T> ok(String msg) {
        return RestUtil(RestContentType.SUCCESS.getCode(), msg,null);
    }

    public static <T> R<T> ok(String msg, T data) {
        return RestUtil(RestContentType.SUCCESS.getCode(), msg,data);
    }

    public static <T> R<T> ok(T data) {
        return  RestUtil(RestContentType.SUCCESS.getCode(), RestContentType.SUCCESS.getMsg(),data);
    }

    public static <T> R<T> failure() {
        return  RestUtil(RestContentType.FAIL.getCode(), RestContentType.FAIL.getMsg(), null);
    }

    public static <T> R<T> failure(T data) {
        return  RestUtil(RestContentType.FAIL.getCode(), RestContentType.FAIL.getMsg(),data);
    }

    public static <T> R<T> error() {
        return  RestUtil(RestContentType.ERROR.getCode(), RestContentType.ERROR.getMsg(), null);
    }

    public static <T> R<T> error(T data) {
        return  RestUtil(RestContentType.ERROR.getCode(), RestContentType.ERROR.getMsg(),data);
    }
    public static <T> R<T> RestUtil(int code,String msg, T data) {
        R<T> apiResponse = new R<T>();
        apiResponse.setCode(code);
        apiResponse.setMsg(msg);
        apiResponse.setData(data);
        return apiResponse;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setData(T data) {
        this.data = data;
    }
}
